{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from xgboost import XGBClassifier\n",
    "import xgboost as xgb\n",
    "\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "from sklearn.metrics import accuracy_score\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最终确定的XGBoost的参数如下：\n",
    "\n",
    "学习率不能超过0.5，测试过，在0.5的情况下，仅仅需要16个学习器就可以在训练集上的accurancy为93%。如果设置为1，那只需要6个学习器了。估计这样的模型在测试集上效果一定不好。尽管在训练集上准确率高。最终选取0.3作为学习率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#确定行列的重采样参数, 这是一个三分类的问题 [2 1 0] 为高、中、低三类\n",
    "xgb1 = XGBClassifier(\n",
    "    learning_rate=0.5,   #调整学习率从0.1 训练集上的准确率为 80.97% .学习率为0.3. 准确率为 88% . 学习率为0.5，准确率为\n",
    "    n_estimators=150,    #第二次确定的学习器数目\n",
    "    max_depth=7,         #确定的树的最大深度\n",
    "    min_child_weight=5,\n",
    "    gamma=0,\n",
    "    subsample=0.8,\n",
    "    colsample_bytree=0.8,\n",
    "    colsample_bylevel=0.7,\n",
    "    reg_alpha = 1.5,     #调整的正则参数\n",
    "    reg_lambda = 0.5,    #调整的正则参数\n",
    "    objective='multi:softmax',\n",
    "    seed=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 读入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test_data = pd.read_csv('RentListingInquries_FE_test.csv')\n",
    "train_data = pd.read_csv(\"RentListingInquries_FE_train.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# train_mt = xgb.DMatrix(train_data)\n",
    "# test_mt = xgb.DMatrix(test_data)\n",
    "\n",
    "y_train = train_data['interest_level']\n",
    "x_train = train_data.drop(['interest_level'],axis=1)\n",
    "\n",
    "x_test = test_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def train_fit(xgb_,x_train,y_train,early_stopping_rounds=10):\n",
    "    xgb_params = xgb_.get_xgb_params()\n",
    "    #print(xgb_params)\n",
    "    xgb_params['num_class'] = 3\n",
    "    xgtrain = xgb.DMatrix(x_train, label = y_train)\n",
    "    xgtest = xgb.DMatrix(x_test)\n",
    "    fit_result = xgb.train(xgb_params,xgtrain,num_boost_round=150)\n",
    "    train_hat = fit_result.predict(xgtrain)\n",
    "    train_predictions = [round(value) for value in train_hat]\n",
    "    #print(train_predictions)\n",
    "    y_train = xgtrain.get_label()\n",
    "    train_accuracy = accuracy_score(y_train, train_predictions)\n",
    "    print(\"Train Accuary: %.2f%%\" % (train_accuracy * 100.0))\n",
    "\n",
    "    test_hat = fit_result.predict(xgtest)\n",
    "    #print(test_hat.T)\n",
    "\n",
    "    x_test['interest'] = test_hat.reshape(-1,1)\n",
    "    print(x_test.head(3))\n",
    "    x_test.to_csv(\"RentListingInquries_FE_result_learning_rate_0.5.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time.struct_time(tm_year=2018, tm_mon=5, tm_mday=29, tm_hour=10, tm_min=8, tm_sec=5, tm_wday=1, tm_yday=149, tm_isdst=0)\n",
      "Train Accuary: 93.01%\n",
      "   bathrooms  bedrooms  price  price_bathrooms  price_bedrooms  room_diff  \\\n",
      "0        1.0         1   2950           1475.0          1475.0        0.0   \n",
      "1        1.0         2   2850           1425.0           950.0       -1.0   \n",
      "2        1.0         1   3758           1879.0          1879.0        0.0   \n",
      "\n",
      "   room_num  Year  Month  Day    ...     walk  walls  war  washer  water  \\\n",
      "0       2.0  2016      6   11    ...        0      0    0       0      0   \n",
      "1       3.0  2016      6   24    ...        0      0    1       0      0   \n",
      "2       2.0  2016      6    3    ...        0      0    0       0      0   \n",
      "\n",
      "   wheelchair  wifi  windows  work  interest  \n",
      "0           0     0        0     0       2.0  \n",
      "1           0     0        0     0       0.0  \n",
      "2           0     0        0     0       2.0  \n",
      "\n",
      "[3 rows x 228 columns]\n",
      "time.struct_time(tm_year=2018, tm_mon=5, tm_mday=29, tm_hour=10, tm_min=11, tm_sec=33, tm_wday=1, tm_yday=149, tm_isdst=0)\n"
     ]
    }
   ],
   "source": [
    "print(time.localtime(time.time()))\n",
    "train_fit(xgb1, x_train, y_train)\n",
    "print(time.localtime(time.time()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
